﻿using huaxin_Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data.SqlClient;
using System.Data;

namespace huaxin_DAO
{
    public class huaxin_PayOrderDao
    {

        public PayOrder GetByOrderId(string orderId)
        {
            using (var conn = SqlConn.OpenConnectionStar())
            {
                string sql = "select top 1 * from payorder where orderid = @orderid";
                var result = conn.QueryFirstOrDefault<PayOrder>(sql, new { orderid = orderId });
                return result;
            }
        }


        /// <summary>
        /// 添加订单处理
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddOrder(huaxin_order order)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into huaxin_order(");
            strSql.Append("OpenId,OrderId,PaymentAmount,PayStyle,PayStatus,");
            strSql.Append("PublishDate,Phone,PrizeQQ,ProductId,");
            strSql.Append("Ip,TimeStart,TimeExpire,IsDel,Remak,Platform)");
            strSql.Append(" values (");
            strSql.Append("@OpenId,@OrderId,@PaymentAmount,");
            strSql.Append("@PayStyle,@PayStatus,@PublishDate,");
            strSql.Append("@Phone,@PrizeQQ,@ProductId,@Ip,");
            strSql.Append("@TimeStart,@TimeExpire,0,@Remak,@Platform)");

            using (IDbConnection conn = SqlConn.OpenConnectionStar())
            {
                return conn.Execute(strSql.ToString(), new
                {
                    OpenId= order.OpenId,
                    OrderId= order.OrderId,
                    PaymentAmount= order.PaymentAmount,
                    PayStyle=order.PayStyle,
                    PayStatus=order.PayStatus??2,
                    PublishDate=DateTime.Now,
                    Phone=order.Phone,
                    PrizeQQ=order.PrizeQQ,
                    ProductId=order.ProductId,
                    Ip=order.Ip,
                    TimeStart=order.TimeStart,
                    TimeExpire=order.TimeExpire,
                    Remak=order.Remark,
                    Platform=order.Platform
                });
            }

        }

        /// <summary>
        /// 订单号处理
        /// </summary>
        /// <param name="orderNo"></param>
        /// <param name="transactionId"></param>
        /// <returns></returns>
        public int OrderNotifyPay(string orderNo,string transactionId)
        {
            using (IDbConnection conn = SqlConn.OpenConnectionStar())
            {
                return conn.QueryFirstOrDefault<int>("Pro_OrderNotify", new
                {
                    out_trade_no = orderNo,
                    Transaction_id= transactionId
                }, null, null, CommandType.StoredProcedure);
            }
        }

        /// <summary>
        /// 根据订单号获取订单详细信息
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public huaxin_order GetOrderIdByInfo(string orderId)
        {
            using (var conn = SqlConn.OpenConnectionStar())
            {
                string sql = "select top 1 * from huaxin_order where orderid = @orderid";
                var result = conn.QueryFirstOrDefault<huaxin_order>(sql, new { orderid = orderId });
                return result;
            }
        }
    }
}
